package com.zhuss.product.processor;

import java.util.Date;
import java.util.List;

import com.zhuss.product.processor.cache.LineCache;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.zhuss.bean.bo.product.line.UpdateLineBo;
import com.zhuss.bean.unit.linesite.BoardBean;
import com.zhuss.bean.vo.product.line.DetailLineVo;
import com.zhuss.common.AbstractProcessor;
import com.zhuss.common.Processor;
import com.zhuss.db.entity.OpLog;
import com.zhuss.enums.LogActionEnum;
import com.zhuss.enums.MoudleEnum;
import com.zhuss.enums.新线招募类型;
import com.zhuss.exception.DzcxException;
import com.zhuss.exception.ExceptionCode;
import com.zhuss.product.db.dao.ext.LineDao;
import com.zhuss.product.db.entity.Line;
import com.zhuss.util.JsonUtil;

/**
 * @description: 线路修改
 * @author: zhushanshuo
 * @create: Wed Dec 30 13:08:49 CST 2020
 **/
@Component("lineUpdate")
public class LineUpdate extends AbstractProcessor<Object, UpdateLineBo> implements Processor<Object, UpdateLineBo> {

    @Autowired
    private LineDao lineDao;

    @Autowired
	@Qualifier("lineRead")
	private LineRead lineRead;

	@Autowired
	@Qualifier("lineCache")
	private LineCache lineCache;

    /**
     * 校验
     */
    @Override
    public void check(UpdateLineBo bo) throws DzcxException {
    	// TODO 修改 线路 校验， 请补充

    	Long lineId = bo.getLineId();
    	String name = bo.getName();
    	if (lineId == null || lineId <= 0l) {
    		throw new DzcxException(ExceptionCode.线路ID为空);
    	}
    	if (name == null || "".equals(name.trim())) {
    		 throw new DzcxException(ExceptionCode.线路name为空);
    	}

		if (bo.getLineNo() == null || "".equals(bo.getLineNo())) {
			throw new DzcxException(ExceptionCode.线路编号为空);
		}

		if (bo.getType() == null || bo.getType() ==0) {
			throw new DzcxException(ExceptionCode.线路类型为空);
		}

		if (bo.getRideType() == null || bo.getRideType() ==0) {
			throw new DzcxException(ExceptionCode.出行方式为空);
		}
    	List<Line> list = lineRead.listNotId(name, lineId, bo.getLineNo());
    	if (!list.isEmpty()) {
    		 throw new DzcxException(ExceptionCode.线路name已存在);
    	}
    }

    /**
     * 操作方法
     * @throws DzcxException 
     */
    @Override
    public Object process(UpdateLineBo param) throws DzcxException {
    	Long lineId = param.getLineId();
    	DetailLineVo line = lineRead.detail(lineId);
    	
    	Line record = new Line();
    	record.setLineId(param.getLineId());
    	record.setName(param.getName());
    	record.setLineNo(param.getLineNo());
    	record.setType(param.getType());
    	record.setStartPointName(param.getStartPointName());
    	record.setEndPointName(param.getEndPointName());
    	record.setFirstTime(param.getFirstTime());
    	record.setRideType(param.getRideType());
    	record.setStatus(param.getStatus());
    	record.setPeopleQuantity(param.getPeopleQuantity());
    	record.setPrice(param.getPrice());
		record.setDistance(param.getDistance());
    	record.setSort(param.getSort());
    	
    	List<BoardBean> boardList = param.getBoardList();
		if (boardList != null) {
			record.setBoard(JsonUtil.toJson(param.getBoardList()));
		}
		
		if (新线招募类型.新线招募.sameKey(line.getPretype())) {
			if (boardList == null || boardList.isEmpty()) {
				throw new DzcxException(ExceptionCode.新线招募班次不能为空);
			}
			if (boardList.size() > 1) {
				throw new DzcxException(ExceptionCode.新线招募班次不能多班次);
			}
		}

    	record.setModifierId(param.getConcurrentUserId());
    	record.setModifier(param.getConcurrentUserName());
    	record.setModifyTime(new Date());
    	lineDao.updateByPrimaryKeySelective(record);

		lineCache.clear(record.getLineId());
        return null;
    }

    /**
     * 操作日志
     */
	@Override
	public Object log(UpdateLineBo param) throws DzcxException {
		OpLog record = new OpLog();
    	record.setMoudle(MoudleEnum.商品.key());
    	record.setAction(LogActionEnum.线路修改.key());
    	record.setTargetId(param.getLineId());
    	record.setTargetName(param.getName());
    	record.setContent("参数: " + JsonUtil.toJson(param));
    	record.setReason(param.getExceptionReason());

    	record.setCreatorId(param.getConcurrentUserId());
    	record.setCreator(param.getConcurrentUserName());
    	record.setCreateTime(new Date());
    	log(record);
    	return null;
	}

}
